package com.main;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;

public class Inventory {


	public Vector2[] list;
	Sprite bg;
	public boolean open;

	public Inventory() {


		list = new Vector2[16];
		for (int i = 0; i < list.length;i++) {
			list[i] = new Vector2(0,0);
		}
	}
	public boolean addObject(Object obj, int amount) {
		boolean done = false;
		for (int i = 0; i < list.length;i++) {
			if (StaticInventoryData.objId.get(obj) == list[i].x) {
				list[i].y += amount;
				done = true;
				break;
			}
		}
		if (!done) {
			for (int i = 0; i < list.length;i++) {
				if (0 == list[i].x) {
					list[i].x = StaticInventoryData.objId.get(obj);
					list[i].y = amount;
					done = true;
					break;
				}
			}
		}
		if (done)
			return true;
		return false;
	}	
	public boolean removeObject(StaticInventoryData.GameObject type,int amount) {
		boolean b = contains(type,amount);
		int toRemove = amount;
		if (!b)
			return b;
		for (int i = 0; i < list.length;i++) {
			if (StaticInventoryData.idObj.get(list[i].x) == type) {
				toRemove -= list[i].y;
				list[i].x = StaticInventoryData.nothingID;
				list[i].y = 0;
			}			
		}
		return true;
	}
	public void removeStack(int stack) {
		list[stack].x = 0;
		list[stack].y = 0;
	}
	public void render(SpriteBatch sb) {
		//bg.draw(sb);
		for (int i = 0; i < list.length/4; i++) {
			for (int j = 0; j < list.length/4;j++) {
				if (list[i*4+j].x != 0) {
					StaticInventoryData.invPics[(int)list[i*4+j].x].setPosition(Gdx.graphics.getWidth()-64*4+64*j, 64*3-64*i);
					StaticInventoryData.invPics[(int)list[i*4+j].x].draw(sb);
				}
			}			
		}
	}
	public boolean contains(StaticInventoryData.GameObject type, int amount) {
		int amountInStash = 0;
		for (int i = 0; i < list.length;i++) {
			if (list[i].x == StaticInventoryData.objId.get(type)) {
				System.out.println(i + " " + type);
				amountInStash += list[i].y;
			}				
		}
		System.out.println(type + " " + amountInStash);
		return amountInStash >= amount;
	}
}
